
package com.hs;

import org.apache.commons.lang.StringUtils;

import com.hs.log.HsLogger;

/**
 * Utility class that holds the current Wiki version constant and provides
 * methods for comparing Wiki versions.
 */
public class HsVersion {

	private static final HsLogger logger = HsLogger.getLogger(HsVersion.class.getName());
	private final int major;
	private final int minor;
	private final int patch;

	/** Current software version.  If this differs from the version in the properties an upgrade is performed. */
	public final static String CURRENT_WIKI_VERSION = "1.0.0";

	/**
	 * Constructor to create a new Wiki version object using a version string of
	 * the form "0.3.5".
	 *
	 * @param version A version string of the form "0.3.5".
	 */
	public HsVersion(String version) throws Exception {
		if (StringUtils.isBlank(version)) {
			throw new Exception("Invalid Wiki version: " + version);
		}
		String[] tokens = version.split("\\.");
		if (tokens.length != 3) {
			throw new Exception("Invalid Wiki version: " + version);
		}
		this.major = new Integer(tokens[0]).intValue();
		this.minor = new Integer(tokens[1]).intValue();
		this.patch = new Integer(tokens[2]).intValue();
	}

	/**
	 * Utility method for comparing the current Wiki version with another
	 * version.
	 *
	 * @param version A Wiki version to compare against.
	 * @return Returns <code>true</code> if the current version is older than
	 *  the version being compared against.
	 */
	public boolean before(HsVersion version) {
		return this.before(version.major, version.minor, version.patch);
	}

	/**
	 * Utility method for comparing the current Wiki version with another
	 * version.
	 *
	 * @param major The major version number to compare against.
	 * @param minor The minor version number to compare against.
	 * @param patch The patch level to compare against.
	 * @return Returns <code>true</code> if the current version is older than
	 *  the version being compared against.
	 */
	public boolean before(int major, int minor, int patch) {
		if (this.major < major) {
			return true; // NOPMD
		}
		if (this.major == major && this.minor < minor) {
			return true; // NOPMD
		}
		if (this.major == major && this.minor == minor && this.patch < patch) {
			return true; // NOPMD
		}
		return false;
	}
}
